/**
 *
 * @author yuan_rz
 * @date 2019/03/12 14:50
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {

        },
        table_where = {

        },
        cols = [[
              {type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'ROW_ID', width: 60, title: '序号', fixed: 'left',hide : true}
            , {
                width: 100,
                title: '',
                toolbar:  '#script-loadlog-detail',
                align: 'center',
                fixed: 'left'
              }
            , {field: 'LOAD_TYPE', width: 100, title: '同步类型', fixed: 'left', sort: true}
            , {field: 'START_TIME', width: 100, title: '开始时间'}
            , {field: 'END_TIME', width: 100, title: '结束时间'}
            , {field: 'USER_NAME', width: 100, title: '操作人'}
            , {field: 'IS_SUCCESS', width: 60, title: '是否成功完成'}
            , {field: 'ERROR_COUNT', width: 60, title: '错误数量'}
            , {
                width: 60,
                title: '',
                toolbar: '#script-loadlogsetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        cols_detail = [[
              {field: 'ROW_ID', width: 60, title: '序号', fixed: 'left'}
            , {field: 'ERROR_DESC', width: 300, title: '错误描述'}
        ]],
        loadlogSettingAction,
        moduleName = '同步日志',
        selectObj
    ;

    var loadlog = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'loadlog'
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'loadlog_table'),
                    url: g_ctxPath + 'sys/loadlog/getListData',
                    exportsUrl: g_ctxPath + 'sys/loadlog/exportListData',
                    exportsName: moduleName + '数据',
                    title: moduleName + '表',
                    id: 'loadlog_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'], //, 'exports'
                    height: '300px',
                    cols: cols,
                    page: true,
                    where: table_where,
                    done: function(res, curr, count){

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-loadlogsetting-action').innerHTML).render({}, function (string) {
                    loadlogSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(loadlog_table)', function (obj) {
                    switch(obj.event) {
                        case 'select':
                            selectObj = obj.data;
                            self.reloadTableDetail();
                            break;
                        case 'detail':
                            layer.tips(loadlogSettingAction, obj.td, {
                                tips: [3, '']
                                , time: 3000
                                , anim: 500
                                , maxWidth: 120
                                , skin: 'layui-table-tips'
                                , success: function (layero, index) {
                                    tipsIndex = index;
                                    layero.find('li').on('click', function () {
                                        switch ($(this).attr('inx')) {
                                            case '2':
                                                self.deleteData(obj);
                                                break;
                                        }
                                        layer.close(index);
                                        tipsIndex = null;
                                    });
                                }
                            });
                            return false;
                            break;
                    }
                });

                //监听行点击事件
                table.on('row(loadlog_table)', function (obj) {
                    selectObj = obj.data;
                    self.reloadTableDetail();

                });

                //监听事件
                table.on('toolbar(loadlog_table)', function (obj) {
                    switch (obj.event) {
                        case 'del':
                            self.deleteDataBatch();
                            break;
                        case 'empty':
                            self.deleteDataAll();
                            break;
                    }
                });
                self.initTableDetail();
            });
        },
        initTableDetail: function(){
            table.render({
                elem: app.getByid(doc, 'loadlog_table_detail'),
                url: g_ctxPath + 'sys/loadlog/getListDataDetail',
                title: moduleName + '表',
                method: 'post',
                id: 'loadlog_table_detail',
                height: '300px',
                cols: cols_detail,
                loading: false,
                page: true,
                done: function (res, curr, count) {

                }
            });
        },
        reloadTable: function () {
            self._reloadTable({id: 'loadlog_table', where: table_where});
            self.reloadTableDetail();
        },
        reloadTableDetail: function () {
            self._reloadTable({id: 'loadlog_table_detail', where: {
                ID : selectObj.ID
            }});
        },
        deleteData: function (obj) {
            app.ajax({
                xurl: 'sys/loadlog/deleteData',
                xdata: obj.data,
                loading: true,
                confirm: true,
                okmsg: moduleName +'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTable();
                    }
                }
            });
        },
        deleteDataBatch: function(){
            var checkStatus = table.checkStatus('loadlog_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('sys_loadlog');
                    _temp.ID = v.ID;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'sys/loadlog/deleteDataBatch',
                    xdata: _xdata,
                    confirm: true,
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName +'信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTable();
                        }
                    }
                });
            }
        },
        deleteDataAll: function () {
            app.ajax({
                xurl: 'sys/loadlog/deleteDataAll',
                xdata: {},
                confirm: true,
                loading: true,
                okmsg: moduleName +'信息清空成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTable();
                    }
                }
            });
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    methods: {

                    }
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new loadlog();

});